package edu.upf.opencities.web.action;

import edu.upf.opencities.datamodel.*;
import edu.upf.opencities.web.manager.impl.ManagerImpl;
import edu.upf.opencities.web.utils.Utils;

/**
 * 
 * @author Victor Valls
 */
public class UserAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private User user;
	private Service service;
	private Utils util = Utils.getInstance();
	private ManagerImpl manager = ManagerImpl.getInstace();

	// Login
	private String loginEmail;
	private String loginPassword;

	private String isLogin;
	private String isSignUp;
	private String isProfileUpdate;
	
	private String confirmEmail;
	private String confirmPassword;

	

	// LOGIN
	public String login() {
		user = new User();
		user.setEmail(loginEmail);
		user.setPassword(loginPassword);
		if (true == manager.loginUser(user)) {
			session.put("user", user);
			return SUCCESS;
		}
		return util.returnError("Wrong username or password");
	}

	// LOGOUT
	public String logout() {
		try {
			session.clear();
			return SUCCESS;
		} catch (Exception e) {
		}
		return util.returnError("Logout ERROR");
	}

	// EDIT PROFILE
	public String editProfile() {
		User sessionUser = (User) session.get("user");
		user = manager.getUser(sessionUser);
		user.setPassword(null);
		if (null != user) {
			return SUCCESS;
		}
		return util.returnError("Unable to load profile");
	}
	
	public String updateProfile() {
		user.setPassword(((User)session.get("user")).getPassword());
		if (true == manager.userUpdate(user)) {
			session.clear();
			session.put("user", user);
			return SUCCESS;
		}
		return util.returnError("A problem occurred editing the profile");
	}



	// CREATE NEW USER
	public String newUser() {
		if ((manager.registerUser(user))) {
			session.put("user", user);
			return SUCCESS;
		}
		return util.returnError("There are some problems in the Sign Up");
	}

	// VALIDATION

	public void validate() {

		try {
			if (getIsLogin().equals("true")) {
				if (loginEmail.length() == 0) {
					addFieldError("loginEmail", "email is required");
				}
				if (loginPassword.length() == 0) {
					addFieldError("loginPassword", "password is required");
				}
			}
			else if (getIsSignUp().equals("true")) {
				if (getUser().getEmail().length() == 0) {
					addFieldError("user.email", "email is required");
				}
				if (getUser().getPassword().length() == 0) {
					addFieldError("user.password", "password is required");
				}
				
				if (!getUser().getEmail().equals(confirmEmail)) {
					addFieldError("confirmEmail", "emails do not match");
				}	
				
				if (!getUser().getPassword().equals(confirmPassword)) {
					addFieldError("confirmPassword", "passwords do not match");
				}
				
				if (getUser().getName().length() == 0) {
					addFieldError("user.name", "name is required");
				}
				if (getUser().getLastName().length() == 0) {
					addFieldError("user.lastName", "last name is required");
				}
				if (getUser().getCompany().length() == 0) {
					addFieldError("user.company", "company is required");
				}
				if (getUser().getCountry().length() == 0) {
					addFieldError("user.country", "country is required");
				}
			}
			else if(getIsProfileUpdate().equals("true")){
				if (getUser().getEmail().length() == 0) {
					addFieldError("user.email", "email is required");
				}
				if (getUser().getPassword().length() == 0) {
					addFieldError("user.password", "password is required");
				}
				if (getUser().getName().length() == 0) {
					addFieldError("user.name", "name is required");
				}
				if (getUser().getLastName().length() == 0) {
					addFieldError("user.lastName", "last name is required");
				}
				if (getUser().getCompany().length() == 0) {
					addFieldError("user.company", "company is required");
				}
				if (getUser().getCountry().length() == 0) {
					addFieldError("user.country", "country is required");
				}
			}
			else if (getIsProfileUpdate().equals("true")) {
				if (getUser().getEmail().length() == 0) {
					addFieldError("user.email", "email is required");
				}
				if (getUser().getName().length() == 0) {
					addFieldError("user.name", "name is required");
				}
				if (getUser().getLastName().length() == 0) {
					addFieldError("user.lastName", "last name is required");
				}
				if (getUser().getCompany().length() == 0) {
					addFieldError("user.company", "company is required");
				}
				if (getUser().getCountry().length() == 0) {
					addFieldError("user.country", "country is required");
				}
			}		
		} catch (Exception e) {
		}
	}

	// GETTERS AND SETTERS
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Service getService() {
		return service;
	}

	public void setService(Service service) {
		this.service = service;
	}

	public String getLoginPassword() {
		return loginPassword;
	}

	public void setLoginPassword(String loginPassword) {
		this.loginPassword = loginPassword;
	}

	public String getLoginEmail() {
		return loginEmail;
	}

	public void setLoginEmail(String loginEmail) {
		this.loginEmail = loginEmail;
	}

	public String getIsLogin() {
		return isLogin;
	}

	public void setIsLogin(String isLogin) {
		this.isLogin = isLogin;
	}

	public String getIsSignUp() {
		return isSignUp;
	}

	public void setIsSignUp(String isSignUp) {
		this.isSignUp = isSignUp;
	}

	public String getIsProfileUpdate() {
		return isProfileUpdate;
	}

	public void setIsProfileUpdate(String isProfileUpdate) {
		this.isProfileUpdate = isProfileUpdate;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public String getConfirmEmail() {
		return confirmEmail;
	}

	public void setConfirmEmail(String confirmEmail) {
		this.confirmEmail = confirmEmail;
	}
}
